package com.aek.ebey.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aek.common.core.base.BaseServiceImpl;
import com.aek.ebey.sys.core.BeanMapper;
import com.aek.ebey.sys.mapper.SysPermissionMapper;
import com.aek.ebey.sys.model.SysModule;
import com.aek.ebey.sys.model.SysPermission;
import com.aek.ebey.sys.model.vo.SysPermissionVo;
import com.aek.ebey.sys.service.SysModuleService;
import com.aek.ebey.sys.service.SysPermissionService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.google.common.collect.Lists;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author aek
 * @since 2017-05-06
 */
@Service
public class SysPermissionServiceImpl extends BaseServiceImpl<SysPermissionMapper, SysPermission>
		implements SysPermissionService {
	@Autowired
	private SysModuleService moduleService;

	@Autowired
	private SysPermissionMapper sysPermissionMapper;

	@Override
	public List<SysPermission> findPermissionByTenantIdWithCan(Long tenantId) {
		// 租户可拥有的模块
		List<SysModule> modules = moduleService.findModuleByTenantIdWithCan(tenantId);

		StringBuilder moduleIds = new StringBuilder();
		if (!modules.isEmpty() && modules.size() > 1) {
			for (SysModule module : modules) {
				moduleIds.append(module.getId());
				moduleIds.append(",");
			}
			moduleIds.deleteCharAt(moduleIds.length() - 1);
		}

		// 根据可拥有模块查询权限
		Wrapper<SysPermission> wrapper = new EntityWrapper<SysPermission>();
		wrapper.in("module_id", moduleIds.toString());

		return this.selectList(wrapper);
	}

	@Override
	public List<SysPermissionVo> findMenuByRoleIdAndModuleId(Long roleId, Long moduleId) {
		return this.sysPermissionMapper.selectMenuByRoleIdAndModuleId(roleId, moduleId);
	}

	@Override
	public List<SysPermissionVo> findByMenuIdAndRoleId(Long menuId, Long roleId) {
		return this.sysPermissionMapper.selectBuMenuIdAndRoleId(roleId, menuId);
	}

	@Override
	public List<SysPermissionVo> findByModuleIdAndRoleIdWithNotInMenu(Long roleId, Long moduleId) {
		return this.sysPermissionMapper.selectByModuleIdAndRoleIdWithNotInMenu(roleId, moduleId);
	}

	@Override
	public List<SysPermission> findMenuByModuleId(Long moduleId) {
		Wrapper<SysPermission> wrapper = new EntityWrapper<SysPermission>();
		wrapper.eq("module_id", moduleId).eq("menu_flag", 1).eq("enable", 1);
		return this.selectList(wrapper);
	}

	@Override
	public List<SysPermissionVo> findByMenuId(Long menuId) {

		Wrapper<SysPermission> wrapper = new EntityWrapper<SysPermission>();
		wrapper.eq("parent_id", menuId).eq("menu_flag", 0).eq("enable", 1);

		List<SysPermission> permissions = this.selectList(wrapper);
		List<SysPermissionVo> permissionVos = Lists.newArrayList();
		for (SysPermission permission : permissions) {
			SysPermissionVo vo = BeanMapper.map(permission, SysPermissionVo.class);
			vo.setEnable(false);
			vo.setDelFlag(true);
			permissionVos.add(vo);
		}
		return permissionVos;
	}

	@Override
	public List<SysPermissionVo> findByModuleIdWithNotMenu(Long moduleId) {

		Wrapper<SysPermission> wrapper = new EntityWrapper<SysPermission>();
		wrapper.eq("module_id", moduleId).eq("menu_flag", 0).eq("enable", 1).le("parent_id", 0);

		List<SysPermission> permissions = this.selectList(wrapper);
		List<SysPermissionVo> permissionVos = Lists.newArrayList();

		for (SysPermission permission : permissions) {
			SysPermissionVo vo = BeanMapper.map(permission, SysPermissionVo.class);
			vo.setEnable(false);
			vo.setDelFlag(true);
			permissionVos.add(vo);
		}
		return permissionVos;
	}

	@Override
	public List<SysPermission> findServicesByUserId(Long userId) {
		return this.sysPermissionMapper.selectServicesByUserId(userId);
	}

	@Override
	public List<SysPermission> findByModuleId(Long moduleId) {
		Wrapper<SysPermission> wrapper = new EntityWrapper<SysPermission>();
		wrapper.eq("module_id", moduleId).eq("enable", 1);
		return this.selectList(wrapper);
	}

}
